﻿using System;
using System.Collections.Generic;

namespace SBPweb.Modules.Smartparts
{
    /// <summary>
    /// Base class for the SmartpartList.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class SmartpartListBase : List<ISmartpart> //<T> : List<T>, ISmartpartList where T : ISmartpart
    {
        
        #region Constructor logic

        /// <summary>
        /// Initizalizes a new SmartpartListBase instance
        /// </summary>
		protected SmartpartListBase()
        {

        }

        #endregion

        #region ISmartpartList Members

        /// <summary>
        /// Gets an array of the ISmartpart items.
        /// </summary>
        public ISmartpart[] Items
        {
            get
            {
                return base.ToArray(); //Array.ConvertAll<T, ISmartpart>(base.ToArray(), new Converter<T, ISmartpart>(ConvertTToISmartpart));
            }
        }

        /// <summary>
        /// Occurs when a new item is added to the list.
        /// </summary>
        public event EventHandler<SmartpartEventArgs> ItemAdded;

        /// <summary>
        /// Occurs when an item is removed from the list.
        /// </summary>
        public event EventHandler<SmartpartEventArgs> ItemRemoved;

        #endregion

        #region Helper methods

        /// <summary>
        /// Returns a converted instance of the parameter.
        /// </summary>
        /// <param name="t">The generic type.</param>
        /// <returns></returns>
        //public static ISmartpart ConvertTToISmartpart(ISmartpart t)
        //{
        //    return (ISmartpart)t;
        //}

        #endregion

        #region IList<ISmartpart> Members

        /// <summary>
        /// Returns the index of the given smartpart.
        /// </summary>
        /// <param name="item">The ISmartpart to look for.</param>
        /// <returns></returns>
        //public int IndexOf(ISmartpart item)
        //{
        //    return base.IndexOf(/*(T)*/item);
        //}

        /// <summary>
        /// Inserts a new smartpart to the specified position.
        /// </summary>
        /// <param name="index">The position of the new smartpart.</param>
        /// <param name="item">The nem smartpart instance.</param>
        public new void Insert(int index, ISmartpart item)
        {
            base.Insert(index, /*(T)*/item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new SmartpartEventArgs(item));
            }
        }

        /// <summary>
        /// Indexer for the list. Gets or sets the specified smartpart.
        /// </summary>
        /// <param name="index">The spcified position in the list.</param>
        /// <returns></returns>
        public new ISmartpart this[int index]
        {
            get
            {
                return (ISmartpart)base[index];
            }
            set
            {
                base[index] = /*(T)*/value;
            }
        }

        #endregion

        #region ICollection<ISmartpart> Members

        /// <summary>
        /// Adds the specified item to the list.
        /// </summary>
        /// <param name="item">The smartpart instance to be added.</param>
        public new void Add(ISmartpart item)
        {
            base.Add(/*(T)*/item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new SmartpartEventArgs(item));
            }
        }

        /// <summary>
        /// Indicates whether the list contains the specified item.
        /// </summary>
        /// <param name="item">The menu item to look for.</param>
        /// <returns></returns>
        //public new bool Contains(ISmartpart item)
        //{
        //    return base.Contains(/*(T)*/item);
        //}

        /// <summary>
        /// This method is not implemented yet!
        /// </summary>
        /// <param name="array">The source array.</param>
        /// <param name="arrayIndex">The starting index.</param>
        //public void CopyTo(ISmartpart[] array, int arrayIndex)
        //{
        //    throw new NotImplementedException();
        //}

        /// <summary>
        /// Gets a value indicating whether the list is read only.
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Removes the specified item from the list.
        /// </summary>
        /// <param name="item">The menuitem to be removed.</param>
        /// <returns></returns>
        public new bool Remove(ISmartpart item)
        {
            bool ret = base.Remove(/*(T)*/item);
            if (ItemRemoved != null)
            {
                ItemRemoved.Invoke(this, new SmartpartEventArgs(item));
            }
            return ret;
        }

        #endregion

        #region IEnumerable<ISmartpart> Members

        /// <summary>
        /// Gets the enumeration of the list.
        /// </summary>
        /// <returns></returns>
        //public new IEnumerator<ISmartpart> GetEnumerator()
        //{
        //    throw new NotImplementedException();
        //}

        #endregion

    }

}
